﻿using Dos.Common;
using MallWeb.Common;
using MallWeb.Model.DbModel;
using MallWeb.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MallWeb.Controllers
{
    /// <summary>
    /// 用户中心
    /// </summary>
    public class UserController : Controller
    {
        // GET: User
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <returns></returns>
        public ActionResult Register() {
            UserView user = new UserView();
            ViewBag.Create = user;
            ViewBag.Login = user;
            return View("Login");
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns></returns>
        public ActionResult Login() {
            UserView user = new UserView();
            ViewBag.Create = user;
            ViewBag.Login = user;
            ViewBag.msg = Session["hello"];
            return View();
        }

        [HttpPost]
        /// <summary>
        /// 注册结果
        /// </summary>
        /// <returns></returns>
        public ActionResult Register(UserView model) {
            UserView user = new UserView();
            ViewBag.Login = user;
            ViewBag.Create = user;
            try
            {
                if (model != null)
                {
                    ViewBag.Create = model;
                    if (String.IsNullOrEmpty(model.username) || String.IsNullOrEmpty(model.password) || String.IsNullOrEmpty(model.conpassword) || String.IsNullOrEmpty(model.checkcode))
                    {
                        model.msg = "信息不完整";
                        return View("Login");
                    }
                    string checkCode = Session["checkCode"].ToString();
                    var config = new CaptchaControl();
                    if (config.CaptchaIsExpires())
                    {
                        model.msg = "Session已经过期";
                        return View("Login");
                    }
                    if (!config.CaptchaValidate(model.checkcode))
                    {
                        model.msg = "验证码不正确";
                        return View("Login");
                    }
                    if (model.password != model.conpassword)
                    {
                        model.msg = "两次输入的密码不一样";
                        return View("Login");
                    }
                    Shop_user shopUser = new Shop_user();
                    shopUser.Username = model.username;
                    shopUser.Password = model.password;
                    shopUser.Password = EncryptHelper.MD5Encrypt(shopUser.Password, 32);
                    int count = MyDB.Context.Insert<Shop_user>(shopUser);
                    if (count > 0)
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
            }
            catch (Exception Err)
            {
                LogHelper.Error(Err.ToString());
                throw Err;
            }
            return View("Login");
        }
        [HttpPost]
        /// <summary>
        /// 登录结果
        /// </summary>
        /// <returns></returns>
        public ActionResult Login(UserView model) {
            UserView user = new UserView();
            ViewBag.Login = user;
            ViewBag.Create = user;
            try
            {
                if (model != null)
                {
                    ViewBag.Login = model;
                    if (String.IsNullOrEmpty(model.username) || String.IsNullOrEmpty(model.password)|| String.IsNullOrEmpty(model.checkcode))
                    {
                        model.msg = "信息不完整";
                        return View("Login");
                    }
                    string checkCode = Session["checkCode"].ToString();
                    var config = new CaptchaControl();
                    if (config.CaptchaIsExpires())
                    {
                        model.msg = "Session已经过期";
                        return View("Login");
                    }
                    if (!config.CaptchaValidate(model.checkcode))
                    {
                        model.msg = "验证码不正确";
                        return View("Login");
                    }
                    string md5 = EncryptHelper.MD5Encrypt(model.password, 32);
                    Shop_user shop_user=MyDB.Context.From<Shop_user>().Where(m => m.Username == model.username && m.Password == md5).ToFirst();
                    if (shop_user != null) {
                        //登录成功
                        return RedirectToAction("Index", "Home");
                    }
                }
            }
            catch (Exception Err)
            {
                LogHelper.Error(Err.ToString());
                throw Err;
            }
            return View("Login");
        }
    }
}